(function($){
        jQuery.fn.extend({
            slideRightShow: function(speed,callback) {
                return this.each(function() {
                    $(this).show('slide', {direction: 'right'}, speed, callback);
                });
            },
            slideLeftHide: function(speed,callback) {
                return this.each(function() {
                    $(this).hide('slide', {direction: 'left'}, speed, callback);
                });
            },
            slideRightHide: function(speed,callback) {
                return this.each(function() {  
                    $(this).hide('slide', {direction: 'right'}, speed, callback);
                });
            },
            slideLeftShow: function(speed,callback) {
                return this.each(function() {
                    $(this).show('slide', {direction: 'left'}, speed, callback);
                });
            }
        });
	    $.fn.extend({
	        jvitLoadPage: function(option) {
	           
               
                var self = this;
                var currentPage = null;
                var numberPage = 0;
                var currentGroup = null;
                var numberGroup = 0;
                var timeCurrentGroup = 1000;
                var groupRepeat = true;
                var groupTimeout = 10000;
                var getTimeoutGroup = null;
                
                var currentElement = null;
                var getTimeoutSlide = null;
                var sliceEffectType = 'show';
                var timeCurrentSlide = 1000;
                var transitionTime = 1000;
                
                var getTimeoutPage = null;
                
                var changeGroup = false;
                
                var contentHtml = '';
                
                var defaults = {
	                slideProperties: {},
                    autoRepeat: true,
                    autoPlay: true,
                    audioRepeat: true,
                    transitionTime: 1000,
                    pageTimeout: 10000
	            }
	            var options =  $.extend(defaults, option);
                init();
                
                function init()
                {
                    contentHtml = ajaxGetContent(options.pageLink);
                    contentHtml = $.parseJSON(contentHtml);
                    numberGroup = $.map(options.group, function(n, i) { return i; }).length;
                    if(contentHtml == '') {
                        alert('Content empty');
                    }else {
                        currentGroup = 0;
                        currentPage = 0;
                        if(options.group[currentGroup].slideProperties[currentPage].sliceEffectType) {
                            sliceEffectType = options.group[currentGroup].slideProperties[currentPage].sliceEffectType;
                        }
                        if(options.group[currentGroup].slideProperties[currentPage].timeout) {
                            timeCurrentSlide = options.group[currentGroup].slideProperties[currentPage].timeout;
                        }
                        if(options.group[currentGroup].slideProperties[currentPage].transitionTime) {
                            transitionTime = options.group[currentGroup].slideProperties[currentPage].transitionTime;
                        }
                        if(options.group[currentGroup].groupTimeout) {
                            groupTimeout = options.group[currentGroup].groupTimeout;
                        }
                        
                    }
                    var content = contentHtml[currentGroup][currentPage];
                    animationSlide(self, content, sliceEffectType, transitionTime);      
                    getTimeoutSlide = setInterval(autoSlide, timeCurrentSlide); 
                    getTimeoutGroup = setInterval(autoGroup, groupTimeout);
                    
                    /*************video*****************/
                    var html = '';
                    if(options.group[currentGroup].hasOwnProperty('videos'))
                    {
                        var video = options.group[currentGroup].videos;
                        var audioRepeat = '';
                        if(video.audioRepeat == true) {
                            audioRepeat = 'loop="loop"';
                        }
                        var autoPlay = '';
                        if(video.autoPlay == true) {
                            autoPlay = 'autoplay="autoplay"';
                        }
                        html = '<video width="0" height="0" controls '+ audioRepeat + ' ' + autoPlay +'>';
                        for(var i = 0; i<Object.keys(video.filename).length; i++)
                        {
                            html = html + '<source src="'+video.filename[i]+'" type="'+video.type[i]+'">';
                        }
                        html = html + '</video>';
                    }
                    if(html != '')
                        $(".video").html(html);
                        
                    /**********page process********/
                    getTimeoutPage = setInterval(autoPage, options.pageTimeout);
                    
                }
                
                function autoSlide()
                {
                    window.clearInterval(getTimeoutSlide);
                    if(changeGroup) {
                        currentPage = 0;
                    } else {
                        if(options.group[currentGroup].slideProperties[currentPage+1] == undefined)
                        {
                            currentPage = 0;
                        }else {
                            currentPage = currentPage + 1;
                        }
                    }
                    
                    
                    var content = contentHtml[currentGroup][currentPage];
                    if(options.group[currentGroup].slideProperties[currentPage].sliceEffectType) {
                        sliceEffectType = options.group[currentGroup].slideProperties[currentPage].sliceEffectType;
                    }
                    if(options.group[currentGroup].slideProperties[currentPage].timeout) {
                        timeCurrentSlide = options.group[currentGroup].slideProperties[currentPage].timeout;
                    }
                    if(options.group[currentGroup].slideProperties[currentPage].transitionTime) {
                        transitionTime = options.group[currentGroup].slideProperties[currentPage].transitionTime;
                    }
                    
                    mumberPage = $.map(options.group[currentGroup].slideProperties, function(n, i) { return i; }).length;
                    
                    if(changeGroup) {
                        sliceEffectType = options.group[currentGroup].groupEffect;
                    }
                    changeGroup = false;
                    animationSlide(self, content, sliceEffectType, transitionTime);     
                    getTimeoutSlide = setInterval(autoSlide, timeCurrentSlide); 
                    if(options.group[currentGroup].groupRepeat == false && (currentPage + 1 == mumberPage))
                    {
                         window.clearInterval(getTimeoutSlide);
                         console.log("xoa");
                    }
                }
                
                function autoGroup() {
                    changeGroup = true;
                    window.clearInterval(getTimeoutGroup);
                    if(options.group[currentGroup+1] == undefined)
                    {
                        currentGroup = 0;
                    }else {
                        currentGroup = currentGroup + 1;
                    }
                    currentPage = 0;
                    
                    /*************video*****************/
                    var html = '';
                    if(options.group[currentGroup].hasOwnProperty('videos'))
                    {
                        var video = options.group[currentGroup].videos;
                        var audioRepeat = '';
                        if(video.audioRepeat == true) {
                            audioRepeat = 'loop="loop"';
                        }
                        var autoPlay = '';
                        if(video.autoPlay == true) {
                            autoPlay = 'autoplay="autoplay"';
                        }
                        html = '<video width="0" height="0" controls '+ audioRepeat + ' ' + autoPlay +'>';
                        for(var i = 0; i<Object.keys(video.filename).length; i++)
                        {
                            html = html + '<source src="'+video.filename[i]+'" type="'+video.type[i]+'">';
                        }
                        html = html + '</video>';
                    }
                    if(html != '')
                        $(".video").html(html);
                    
                    timeCurrentGroup = options.group[currentGroup].groupTimeout;
                    getTimeoutGroup = setInterval(autoGroup, timeCurrentGroup); 
                    console.log(options.autoRepeat);
                    if(options.autoRepeat == false && (currentGroup + 1 == numberGroup))
                    {
                         window.clearInterval(getTimeoutGroup);
                    }
                }
                
                function autoPage()
                {
                    if(options.pageEffectTimeout == 'stop') {
                        window.clearInterval(getTimeoutPage);
                        pause();
                    }else {
                        if(options.pageLink) {
                            window.location.href = options.pageLink;
                        }
                    }
                }
                
                function animationSlide(self, contentHtml, sliceEffectType, transitionTime) {
                    console.log(sliceEffectType);
                    switch(sliceEffectType) {
                        case 'none':
                            $(self).hide(transitionTime, function(){
                                $(self).html(contentHtml);
                            });
                            break;
                        case 'show':
                            $(self).hide(transitionTime, function(){
                                $(self).html(contentHtml);
                                $(self).show(transitionTime);
                            });
                            //$(self).delay(transitionTime).slideRightShow(transitionTime);
                            //$(self).hide(transitionTime).empty().html(contentHtml).delay(transitionTime).show(transitionTime);   
                            break;
                        case 'fadeIn':
                            //$(self).find('img').effect( 'drop', {}, 500);
                            $(self).fadeOut(transitionTime,function(){
                                $(self).html(contentHtml);
                                $(self).fadeIn(transitionTime);   
                            })
                            /*
                            $(self).slideUp(transitionTime, function(){
                                $(self).html(contentHtml);
                            });
                            */
                            //$(self).effect( 'slide', {}, 500);
                            //$(self).fadeOut(transitionTime).html(contentHtml).fadeIn(transitionTime); 
                            //$(self).slideUp(transitionTime);
                            //$(self).fadeIn(transitionTime).fadeOut().fadeIn();   
                            break;
                        case 'slideDown':
                            /*
                            $(self).slideUp(transitionTime, function(){
                                $(self).html(contentHtml);
                            });
                            */
                            $(self).slideUp(transitionTime, function(){
                                $(self).html(contentHtml);
                                $(self).slideDown(transitionTime);
                            });
                            //$(self).slideDown(transitionTime);
                            //$(self).effect("puff");
                            //$(self).slideUp(transitionTime).empty().html(contentHtml).slideDown(transitionTime);    
                            break;
                        case 'explode':
                            $(self).html(contentHtml);
                            $(self).show('puff',{},1000,function(){console.log("ok")});
                            /*
                            $(self).effect( 'puff', { times:3 }, 2000);
                            $(self).html(contentHtml);
                            $(self).show();
                            */
                            break;
                        case 'slideRightShow':
                            $(self).hide(transitionTime, function(){
                                $(self).html(contentHtml);
                            });
                            $(self).slideRightShow(transitionTime);
                            break;
                        case 'slideLeftShow':
                            $(self).slideRightHide(transitionTime);
                            $(self).hide(transitionTime, function(){
                                $(self).html(contentHtml);
                            });
                            $(self).slideLeftShow(transitionTime);
                            break;
                                
                        default:
                            $(self).html(contentHtml).hide().show(transitionTime);   
                    }
                }
                
                
                function getContent(link)
                {
                    bodyContent = $.ajax({
                          url: link,
                          global: false,
                          type: "POST",
                          dataType: "jsonp",
                          async:false
                       } 
                    ).responseText;
                    return bodyContent;
                }
                
                function start() {
                    autoGroup();
                    autoSlide();
                }
                function pause() {;
                    window.clearInterval(getTimeoutGroup);
                    window.clearInterval(getTimeoutSlide);
                }
                function ajaxGetContent(url) {
                    
                     contentHtml = $.ajax({
                		url: url,
                		type: 'POST',
                		async: false
                	}).responseText;
                    return contentHtml;
                }
                
                
                
                return {
                    start: function start() {
                        autoGroup();
                        autoSlide();
                    },
                    pause: function pause() {;
                        window.clearInterval(getTimeoutGroup);
                        window.clearInterval(getTimeoutSlide);
                    }
                }; 
	        }
	    });       
})(jQuery);



